package books.to.africa

class TeacherRegisterController {

//	def scaffold = true	
    def index = {redirect (action:teacherHome)}
    //def home = {render (view: "teacherRegister.gsp")}
	
	def teacherHome = {render (view: "teacherRegister.gsp")}
	
	def teacherRegister = {
		          def teacherrole = Role.findByAuthority('ROLE_TEACHER')
        //code for registering without photo...
        def newuser = User.findByUsername(params.userName) ?:
                        new TeacherRegister (
                                username: params.userName,
                                password: params.password,
                                enabled: true,
                                email: params.email,
				firstName: params.firstName,
				lastName: params.lastName,
				street: params.street,
				town: params.town,
				country: params.country,
				schoolName: params.schoolName,
				telephoneNumber : params.phonenumber,
				gradeLevel : params.gradelevel
				).save(failOnError:true)

                        UserRole.create newuser, teacherrole
                        redirect (action: confirmed)


}
	def confirmed = {render "New teacher confirmed"}
	
	def requestBook = {
			
		}
	
	def addBookRequest = {
		
		def teacher = TeacherRegister.findByFirstName('zxc')
		def br = new BookRequest(params)
		br.save()
		if( !br.save() ) {
			br.errors.each {
				 println it
			}
		 }
		teacher.addToBookRequests(br)
		teacher.save()
		redirect (action: requestBook)
	}
}
